package org.atlantis.net.base.vote;

/*
 * 投票信息。
 */
public class VoteMessage {

	/* 如果为true则表示该消息是查询请求 ，false表示该消息是投票消息 */
	private boolean isInquiry;
	/* 表示该消息是响应（由服务器发送）还是请求（由客户端发送） */
	private boolean isResponse;
	/* 表示候选人的ID */
	private int candidateID;
	/* 候选人获得的总选票数 */
	private long voteCount;
	/* 最多只有1000个候选人 */
	private static final int MAX_CANDIDATE_ID = 1000;

	public VoteMessage(boolean isResponse, boolean isInquiry, int candidateID,
			long voteCount) {
		if (voteCount != 0 && !isResponse) {
			throw new IllegalArgumentException(
					"Request voteCount paramter must be zero.");
		}
		if (candidateID < 0 || candidateID > MAX_CANDIDATE_ID) {
			throw new IllegalArgumentException("Bad Candidate ID: "
					+ candidateID);
		}
		if (voteCount < 0) {
			throw new IllegalArgumentException("Total must be >= zero.");
		}
		this.isInquiry = isInquiry;
		this.isResponse = isResponse;
		this.candidateID = candidateID;
		this.voteCount = voteCount;
	}

	public boolean isInquiry() {
		return isInquiry;
	}

	public void setInquiry(boolean isInquiry) {
		this.isInquiry = isInquiry;
	}

	public boolean isResponse() {
		return isResponse;
	}

	public void setResponse(boolean isResponse) {
		this.isResponse = isResponse;
	}

	public int getCandidateID() {
		return candidateID;
	}

	public void setCandidateID(int candidateID) {
		if (candidateID < 0 || candidateID > MAX_CANDIDATE_ID) {
			throw new IllegalArgumentException("Bad Candidate ID: "
					+ candidateID);
		}
		this.candidateID = candidateID;
	}

	public long getVoteCount() {
		return voteCount;
	}

	public void setVoteCount(long voteCount) {
		if (voteCount < 0) {
			throw new IllegalArgumentException("Total must be >= zero.");
		}
		this.voteCount = voteCount;
	}

	@Override
	public String toString() {
		return String.format("No.%s号选手现在获得选票%s张", candidateID, voteCount);
	}
}
